package com.wjy.sfhcore.engine;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.PackageInfo;
import android.telephony.TelephonyManager;

import com.wjy.sfhcore.CoreApplication;
import com.wjy.sfhcore.util.OpenUDIDManager;
import com.wjy.sfhcore.util.Tools;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

/**
 * @author wjy
 */
public class BaseParams {
    private static BaseParams instance = new BaseParams();

    private BaseParams() {
    }

    public static BaseParams getInstance() {
        return instance;
    }

    /**
     * 基本参数
     *
     * @return 参数
     */
    public Map<String, String> getBaseParams() {
        Map<String, String> params = new HashMap<String, String>();
        params.put("User-Agent", "saveHelper" + getAppVersion());
        // params.put("Cookie",
        // MyApplication.getInstance().sharedPreferencesFactory
        // .getCookie());
        return params;
    }

    /**
     * 获取手机唯一识别码
     *
     * @return 手机唯一识别码
     */
    public String getImieStatus() {
        Context context = CoreApplication.getInstance();
        SharedPreferences sp = context.getSharedPreferences("config",
                Context.MODE_PRIVATE);
        Editor editor = sp.edit();
        String deviceId = sp.getString("IDID", "").trim();

        if (Tools.isEmpty(deviceId)) {
            // 如果config文件中没有存储IDID，则需要重新获取IDID,首先考虑deviceId
            TelephonyManager tm = (TelephonyManager) context
                    .getSystemService(Context.TELEPHONY_SERVICE);
            deviceId = tm != null ? Tools.trim(tm.getDeviceId()) : "";

            // 如果没有获取到deviceId,则通过getMyUUID获取
            if (Tools.isEmpty(deviceId)) {
                // deviceId = getMyUUID();
                OpenUDIDManager.sync(context);
                if (OpenUDIDManager.isInitialized()) {
                    deviceId = OpenUDIDManager.getOpenUDID();
                }

                if (Tools.isEmpty(deviceId)) {
                    UUID uuid = UUID.randomUUID();
                    String uuidString = uuid.toString();
                    if (!Tools.isEmpty(uuidString) && uuidString.contains("-")) {
                        uuidString = uuidString.replace("-", "");
                    }
                    deviceId = uuidString;
                }

                if (Tools.isEmpty(deviceId)) {
                    // 产生一个8位随机数(10000000~99999999)
                    Random random = new Random();
                    int randomNum = random.nextInt(89999999) + 10000000;
                    deviceId = String.valueOf(randomNum);
                }
            }

            // 保存到config文件中
            editor.putString("IDID", deviceId);
            editor.commit();
        }
        return Tools.trim(deviceId);
    }

    public String getAppVersion() {
        try {
            Context context = CoreApplication.getInstance();
            PackageInfo info = context.getPackageManager().getPackageInfo(
                    context.getPackageName(), 0);
            return info.versionName;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
